package com.study.spmongodb.effjava.demo83.demo14;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import static java.util.Comparator.comparingInt;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/9 16:47
 */
public class Person implements Comparable<Person> {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public Person setAge(int age) {
        this.age = age;
        return this;
    }

    public String getName() {
        return name;
    }

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public static final Comparator<Person> COMPARATOR =
            comparingInt((Person p) -> p.getAge())
                    .thenComparing(p -> p.getName());

    @Override
    public int compareTo(Person o) {
        return COMPARATOR.compare(this, o);
    }

    public static void main(String[] args) {
        final Person person = new Person();
        person.setAge(1).setName("lisi");

        final Person person2 = new Person();
        person2.setAge(2).setName("lisi2");

        System.out.println(person.compareTo(person2));

        final List[] lists = {Arrays.asList(person, person2)};
        System.out.println(Arrays.toString(lists));
        Arrays.sort(lists);
        System.out.println(Arrays.toString(lists));
    }
}
